package com.hzya.frame.utils.help;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import com.hzya.frame.system.user.entity.UserEntity;
import com.hzya.frame.utils.core.action.SessionContext;
import com.hzya.frame.utils.core.tree.TreeNode;

public class TreeNodeHelper {

	private TreeNodeHelper() {
	}

	/***
	 * 平台树根节点
	 * 
	 * @return
	 * @throws Exception
	 */
	public static TreeNode getPlatformRootNode() throws Exception {
	/*	TreeNode root = new TreeNode();
		IOrganizationService organizationService = (IOrganizationService) SpringContextUtil.getBean("hr_organizationService");
		OrganizationDomain platformOrg = organizationService.getPlatformOrg();
		root.setId(-1L);
		root.setParentId(null);
		Map attributesMap = new LinkedHashMap();
		root.setText(platformOrg.getOrg_name());
		attributesMap.put("code", "00");
		root.setAttributes(attributesMap);// 虚拟节点

		root.setState("closed");
		return root;*/
		return null;
	}

	/***
	 * 这里根据用户的类型得到不同的树节点
	 * 
	 * @return 待定
	 * @throws Exception
	 */
	public static TreeNode getRootNode() throws Exception {
		/*UserBean user = SessionContext.getCurrentUser();
		OrganizationDomain organizationDomain = user.getOrganizationDomain();
		TreeNode root = new TreeNode();
		// 获取平台的组织机构id
		root.setParentId(-1L);
		// 获取当前组织机构的id(连锁or单店id)
		root.setId(organizationDomain.getUp_org_id());
		// 组织机构名称
		root.setText(user.getCompany_org_name());
		root.setAttributes("0");// 虚拟节点
		root.setState("closed");
		return root;*/
		return null;
	}

	/***
	 * 组织机构根节点
	 * 
	 * @return
	 * @throws Exception
	 */
	public static synchronized TreeNode getOrgRootNode() throws Exception {
		UserEntity user = SessionContext.getCurrentUser();
		TreeNode root = new TreeNode();
		// 获取平台的组织机构id
		root.setParentId(-1L);
		// 获取当前组织机构的id(连锁or单店id)
		// root.setId(user.getRootOrganizationDomain().getId());
		// 组织机构名称
		//root.setText(user.getCompany_org_name());
		root.setAttributes("0");// 虚拟节点
		root.setState("closed");
		return root;
	}

	/***
	 * 组织机构根节点
	 * 
	 * @return
	 * @throws Exception
	 */
	public static synchronized TreeNode getOrgRootNode(Long nodeId) throws Exception {
		UserEntity user = SessionContext.getCurrentUser();
		TreeNode root = new TreeNode();
		// 获取平台的组织机构id
		root.setParentId(-1L);
		// 获取当前组织机构的id(连锁or单店id)
		root.setId(nodeId);
		// 组织机构名称
		//root.setText(user.getCompany_org_name());
		root.setAttributes("0");// 虚拟节点
		root.setState("closed");
		return root;
	}

	/***
	 * 平台树
	 * 
	 * @return
	 * @throws Exception
	 */
	public static TreeNode buildPlatformRootTree(Map<Long, TreeNode> treeMap) throws Exception {
		TreeNode root = getPlatformRootNode();
		return buildTree(root, treeMap);
	}

	/***
	 * 组织机构树
	 * 
	 * @return
	 * @throws Exception
	 */
	public static TreeNode buildOrgRootTree(Map<Long, TreeNode> treeMap) throws Exception {
		TreeNode root = getOrgRootNode();
		return buildTree(root, treeMap);
	}

	public static TreeNode buildTree(TreeNode root, Map<Long, TreeNode> mapped) {
		Set<Long> keys = mapped.keySet();
		Iterator<Long> it = keys.iterator();
		while (it.hasNext()) {
			TreeNode node = mapped.get(it.next());
			if (null != node.getParentId()) {
				TreeNode parent = mapped.get(node.getParentId());
				if (parent != null) {
					// node.setParent(parent);
					parent.getChildren().add(node);
				} else {
					// node.setParent(root);
					// System.out.println(node.getId()+",");
					root.getChildren().add(node);
				}
			}
		}
		mapped.put(root.getId(), root);
		// Collections.sort(root.getChildren(), comparator);
		return root;
	}
}
